package com.cpi.basilos.warehouse.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.cpi.basilos.warehouse.dto.WarehouseDTO;
import com.cpi.basilos.warehouse.exception.WarehouseException;
import com.cs.common.exception.CSException;
import com.cs.common.pool.DBDAO;	

public class WarehouseDAO extends DBDAO {
	private static org.apache.log4j.Logger logger = Logger.getLogger(WarehouseDAO.class);
	private static final String SQL_GET_WAREHOUSE = "select CODIGO, NOMBRE, INF_ALMACEN, INV_UBICACION, INV_LOTE, INF_COMPANIA from dbo.VW_ALMACEN_BASCULA WHERE ID_ALMACEN = ?";//$NON-NLS-1$
	public WarehouseDTO getWarehouse(int id)throws WarehouseException{
		WarehouseDTO warehouseDTO = null;
		Connection connection =  null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try{
			preparedStatement = prepareSQL(SQL_GET_WAREHOUSE, dsBasilos);
			preparedStatement.setInt(1, id);
			resultSet = executeQuery(preparedStatement);
			warehouseDTO = new WarehouseDTO();
			while(resultSet.next()){
				warehouseDTO.setId(id);
				warehouseDTO.setCode(resultSet.getString(1));
				warehouseDTO.setName(resultSet.getString(2));
				warehouseDTO.setNumber(resultSet.getInt(3));
				warehouseDTO.setLocation(resultSet.getBoolean(4));
				warehouseDTO.setBatch(resultSet.getBoolean(5));
				warehouseDTO.setCompany(resultSet.getInt(6));
			}
			connection = preparedStatement.getConnection();
		}catch (Exception exception) {
			WarehouseException warehouseException = null;
			logger.error(exception);
			exception.printStackTrace(System.err);
			warehouseException = new WarehouseException(exception,CSException.LAYER_DAO, CSException.ACTION_SELECT);
			throw warehouseException;
		}finally{
			try{
				closeConnection(connection);
			}catch(SQLException sqlException){}
		}
		return warehouseDTO;
	}
}